package com.hzqy.commons.service.utils;

import com.hzqy.commons.utils.MD5Util;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.AuthenticatedVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.util.Date;


public class ServiceUtils {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	String cookiesValue = null;
	
	public ServiceUtils(String cookiesValue) {
		this.cookiesValue = cookiesValue;
	}
	
	/**
	 * 鉴权-根据服务ID鉴权
	 * @param serverid
	 * @param serviceCode
	 * @param readTimeOut
	 * @param connectTimeout
	 * @return
	 */
	public String getServiceResult(String serverid,String serviceCode,int readTimeOut,int connectTimeout){
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		Long time = new Date().getTime();
		String Result = "";
		String returnurl = myIp+"service/service.jsp";
//		String url = "http://172.30.0.185:8080/serve/api/buyClient/serviceAuth.do?UserID="+serverid+"&ServiceID="+serviceCode+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
		String serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_path_url.getKey());
		/*if(serverAuthUrl!=null && !serverAuthUrl.endsWith("/"))
			serverAuthUrl += "/";*/
		String url = null;
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			url = serverAuthUrl+"?UserID="+serverid+"&ServiceID="+serviceCode+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
			Result = OrderXmlUtils.sendPost(url, readTimeOut, connectTimeout,this.cookiesValue);
			return Result;
		} catch(Exception e) {
			LOG.error("getServiceResult服务鉴权失败!"+url);
			LOG.error("getServiceResult服务鉴权失败:",e);
			return "-1";
		}
	}
	
	/**
	 * 根据产品ID进行鉴权
	 * @param serverid
	 * @param productCode
	 * @param readTimeOut
	 * @param connectTimeout
	 * @return
	 */
	public String getServiceResultByProductID(AuthenticatedVo authVo){
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		Long time = new Date().getTime();
		String Result = "";
		String returnurl = myIp+"service/service.jsp";
		String contentId="";
		
		//云内网地址,无法访问
		String serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_path_url.getKey());
		/*if(serverAuthUrl!=null && !serverAuthUrl.endsWith("/"))
			serverAuthUrl += "/";*/
		
		//String url = "http://172.30.0.185:8080/serve/api/buyClient/serviceAuth.do?UserID="+serverid+"&ProductID="+productCode+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
		//String url = "http://10.255.247.124:8080/serve/api/buyClient/serviceAuth.do?SPID=9999&UserID="+serverid+"&action=0&ProductID="+productCode+"&Returnurl="+returnurl +"&TimeStamp="+time;
		//String url = "http://172.30.0.185:8080/serve/api/buyClient/serviceAuth.do?SPID=9999&UserID="+serverid+"&action=0&ProductID="+productCode+"&Returnurl="+returnurl +"&TimeStamp="+time;
		String url = null;
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			contentId = authVo.getContentId();
			if(!"".equals(contentId)&&contentId!=null&&!"undefined".equals(contentId)){
				url = serverAuthUrl+"?UserID="+authVo.getServerid()+"&ProductID="+authVo.getProductCode()+"&contentID="+contentId+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
			}else{
				url = serverAuthUrl+"?UserID="+authVo.getServerid()+"&ProductID="+authVo.getProductCode()+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
			}
			LOG.debug("鉴权url= "+url);
			Result = OrderXmlUtils.sendPost(url, authVo.getReadTimeOut(), authVo.getConnectTimeout(),this.cookiesValue);
			return Result;
		} catch(Exception e) {
			LOG.error("getServiceResultByProductID服务鉴权失败!"+url);
			LOG.error("getServiceResultByProductID服务鉴权失败:",e);
			return "-1";
		}
	}
	
	/**
	 * 鉴权-根据服务ID单次鉴权
	 * @param serverid
	 * @param serviceCode
	 * @param readTimeOut
	 * @param connectTimeout
	 * @return
	 */
	public String getServiceResultByContendID(String serverid,String serviceCode,String contendID,int readTimeOut,int connectTimeout){
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		Long time = new Date().getTime();
		String Result = "";
		String returnurl = myIp+"service/service.jsp";
		String serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_path_url.getKey());
		String url = null;
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			url = serverAuthUrl+"?UserID="+serverid+"&ServiceID="+serviceCode+"&ContentID="+contendID+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
			Result = OrderXmlUtils.sendPost(url, readTimeOut, connectTimeout,this.cookiesValue);
			return Result;
		} catch(Exception e) {
			LOG.error("getServiceResultByContendID服务鉴权失败!"+url);
			LOG.error("getServiceResultByContendID服务鉴权失败:",e);
			return "-1";
		}
	}
	
	/**
	 * 根据产品ID进行单次鉴权
	 * @param serverid
	 * @param productCode
	 * @param readTimeOut
	 * @param connectTimeout
	 * @return
	 */
	public String getProductResultByContendID(String serverid,String productCode,String contendID,int readTimeOut,int connectTimeout){
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		Long time = new Date().getTime();
		String Result = "";
		String returnurl = myIp+"service/service.jsp";
		String serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_path_url.getKey());
		String url = null;
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			url = serverAuthUrl+"?UserID="+serverid+"&ProductID="+productCode+"&ContentID="+contendID+"&TimeStamp="+time+"&Action=0&Returnurl="+returnurl;
			LOG.debug("鉴权url= "+url);
			Result = OrderXmlUtils.sendPost(url, readTimeOut, connectTimeout,this.cookiesValue);
			return Result;
		} catch(Exception e) {
			LOG.error("getProductResultByContendID服务鉴权失败!"+url);
			LOG.error("getProductResultByContendID服务鉴权失败:",e);
			return "-1";
		}
	}
	/**
	   *  用户已订购产品详情
	 * @param serverid
	 * @param channelCode
	 * @param token
	 * @param readTimeOut
	 * @param connectTimeout
	 * @return
	 */
	public String getProductResultByunionProductId(String serverid,int readTimeOut,int connectTimeout){
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		String returnurl = myIp+"service/service.jsp";
		String serverAuthUrl = SystemConfigUtils.getSysConfig("serviceUtils_url");
		serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_OrderedProducts_url.getKey());
		String url = null;
		String Result = "";
		String channelCode = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.company.getKey());
		String secreKey = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.secreKey.getKey());
		String token = MD5Util.MD5_32bit(serverid+channelCode+secreKey);
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			url = serverAuthUrl+"?userid="+serverid+"&channelCode="+channelCode+"&token="+token;
			LOG.debug("用户已订购产品详情url= "+url);
			Result = OrderXmlUtils.sendPost(url, readTimeOut, connectTimeout,this.cookiesValue);
			LOG.debug("用户已订购产品详情Result= "+Result);
			return Result;
		} catch (Exception e) {
			LOG.error("用户已订购产品详情查询失败:",e);
			return "-1";
		}
	}
	public String getOrderedProductsBychannelCode(AuthenticatedVo authVo,String server_channelCode,String server_channelCode_pwd){
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		String returnurl = myIp+"service/service.jsp";
		String serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_OrderedProducts_url.getKey());
		String token = MD5Util.MD5_32bit(authVo.getServerid()+server_channelCode+server_channelCode_pwd);
		token.trim();
		String url = null;
		String ResultJson = "";
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			url = serverAuthUrl+"?userid="+authVo.getServerid()+"&channelCode="+server_channelCode+"&token="+token;
			LOG.debug("根据提供的渠道号查询用户相对应的已订购产品详情url="+url+"=authVo.getReadTimeOut()="+authVo.getReadTimeOut()+"=authVo.getConnectTimeout()="+authVo.getConnectTimeout());
			ResultJson = OrderXmlUtils.sendPost(url, authVo.getReadTimeOut(), authVo.getConnectTimeout(),this.cookiesValue);
			LOG.debug("根据提供的渠道号查询用户相对应的已订购产品详情ResultJson="+ResultJson);
			//JSONObject.parseObject(ResultJson)
			return ResultJson ;
		} catch (Exception e) {
			LOG.error("根据提供的渠道号查询用户相对应的已订购产品详情查询失败:",e);
			return "-1";
		}
	}
	/**
	 * 通过批量鉴权接口鉴权
	 * @param authVo
	 * @return
	 */
	public String getServiceResultByBatchInterface(AuthenticatedVo authVo) {
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		String returnurl = myIp+"service/service.jsp";
		String serverAuthUrl = 
				SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_batchauth_interface_url.getKey());
		String server_channelCode = 
				SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.company.getKey());;
		String server_channelCode_pwd = 
				SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.secreKey.getKey());;
		String token = MD5Util.MD5_32bit(authVo.getServerid()+server_channelCode+server_channelCode_pwd);
		token.trim();
		String url = null;
		String ResultJson = "";
		try {
			returnurl = URLEncoder.encode(returnurl,"UTF-8");
			url = serverAuthUrl+"?userid="+authVo.getServerid()+"&channelCode="+server_channelCode+"&token="+token;
			LOG.debug("批量鉴权接口url="+url+"=authVo.getReadTimeOut()="+authVo.getReadTimeOut()+"=authVo.getConnectTimeout()="+authVo.getConnectTimeout());
			ResultJson = OrderXmlUtils.sendPost(url, authVo.getReadTimeOut(), authVo.getConnectTimeout(),this.cookiesValue);
			LOG.debug("通过批量鉴权接口鉴权的结果ResultJson="+ResultJson);
			return ResultJson ;
		} catch (Exception e) {
			LOG.error("根据批量鉴权接口鉴权失败:",e);
			return "-1";
		}
	}
	
	/*
	public String getOrderResult(String SPID,String productCode,String serverid,String partner,String epg_info,int readTimeOut,int connectTimeout) {
		String myIp = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_return_url.getKey());
		String Result = "";
		String returnurl = myIp+"service/service_order_back.jsp";
		returnurl = URLEncoder.encode(returnurl);
		String serverAuthUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.server_auth_path_url.getKey());
		if(serverAuthUrl!=null && !serverAuthUrl.endsWith("/"))
			serverAuthUrl += "/";
//		String url = "http://172.30.0.185:8080/serve/newapp/map.do?SPID=" + SPID +
//				"&ProductID="+productCode+"&Action=1&returnurl=" + returnurl +
//				"&OrderMode=1&ContinueType=1&Partner="+partner+"&UserID="+serverid+"&epg_info="+epg_info;
		String url = serverAuthUrl+"serve/newapp/map.do?SPID=" + SPID +
				"&ProductID="+productCode+"&Action=1&returnurl=" + returnurl +
				"&OrderMode=1&ContinueType=1&Partner="+partner+"&UserID="+serverid+"&epg_info="+epg_info;
		try {
			LOG.debug(url);
			Result = OrderXmlUtils.sendPost(url, readTimeOut, connectTimeout);
			return Result;
		} catch(Exception e) {
			LOG.error("getOrderResult订购失败!"+url);
			LOG.error("getOrderResult订购失败:",e);
			return "-1";
		}
	}
	*/
}
